package cn.ffcs.unm.position.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.EnvPath;
import cn.ffcs.unm.position.action.bean.PostBasicInformationMainBean;
import cn.ffcs.unm.position.manager.PositionDelManager;
import cn.ffcs.unm.position.model.PostBasicInformation;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class PostBasicInformationMainComposer extends BasePortletComposer{

	private static final long serialVersionUID = -8174090381247860644L;

	private PostBasicInformationMainBean bean = new PostBasicInformationMainBean();
	
	private PostBasicInformation qryPostBasicInformation;
	@SuppressWarnings("unused")
	private PostBasicInformation postBasicInformation;
	
	@Autowired
	@Qualifier("positionDelManager")
	private PositionDelManager positionDelManager;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}
	
	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	public void onCreate$postBasicInformationMainWin() throws Exception {
	}
	
	public void onQuery(){
		clearVariable();
		PostBasicInformation objPostBasicInformation = PostBasicInformation.newInstance();
		PubUtil.fillPoFromBean(bean, objPostBasicInformation);
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请输入账期", "温馨提示");
			return;
		}
		int index = bean.getRadiogroup().getSelectedIndex();
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		if (index == 0) {
			String monthIdMonth = monthId.substring(0, 6);
			objPostBasicInformation.setMonthId(monthIdMonth);
		}else {
			objPostBasicInformation.setMonthId(monthId);
		}
		qryPostBasicInformation = objPostBasicInformation;
		this.bean.getPostBasicInformationListboxPaging().setActivePage(0);
		onPostBasicInformationQuery();
	}
	public void onReset() {
		bean.getMonthId().setValue(null);
		bean.getPostCode().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getPostBasicInformationListbox());
		this.bean.getPostBasicInformationListboxPaging().setTotalSize(1);
	}
	public void onPostBasicInformationQuery(){
		if (this.qryPostBasicInformation != null) {
			int activePage = bean.getPostBasicInformationListboxPaging()
					.getActivePage();
			int pageSize = bean.getPostBasicInformationListboxPaging().getPageSize();
			PageInfo pageInfo = positionDelManager
					.queryPageInfoByPostBasicInformation(qryPostBasicInformation,
							activePage + 1, pageSize);
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getPostBasicInformationListbox().setModel(dataList);
			bean.getPostBasicInformationListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}
	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryPostBasicInformation = null;
		postBasicInformation = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onPostBasicInformationQuery();
	}
	
	/**
	 * 岗位基本信息--清单下载
	 * @Description:
	 * @author Wang Yiren
	 * @date 2018年3月12日
	 * @throws Exception
	 * @throws IOException
	 */
	public void onDownload() throws Exception, IOException {
		
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请选择：月表账期", "温馨提示");
			return;
		}
		//日/月表按钮
		int index = bean.getRadiogroup().getSelectedIndex();
		//格式化日期
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		//index == 0 表示月
		if (index == 0) {
			//获取年月--201801
			String monthIdMonth = monthId.substring(0, 6);
			// 获取服务器文件
			String filePath = EnvPath.proPath + monthIdMonth + "_" + "VIEW_DATALIST_OM_POST.csv";
			File file = new File(filePath);
			if (file.exists()) {
				try {
					// 设置字符集
					String charset = "UTF-8";
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode("28岗位基本信息翻译清单.csv", charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					//将文件转化为文件输入流
					FileInputStream in = new FileInputStream(file);
					//掉zk的浏览器下载方法，进行下载
					Filedownload.save(in, "application/octet-stream", encodedName);
				} catch (FileNotFoundException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (IOException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (Exception e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				}
			} else {
				ZkUtil.showError("下载清单失败,请联系管理员", "温馨提示");
			}
		} else {
			ZkUtil.showError("暂不支持日表清单下载", "温馨提示");
		}
	}
	
	
	public void onDownloadNative() throws Exception, IOException {
		
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请选择：月表账期", "温馨提示");
			return;
		}
		//日/月表按钮
		int index = bean.getRadiogroup().getSelectedIndex();
		//格式化日期
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		//index == 0 表示月
		if (index == 0) {
			//获取年月--201801
			String monthIdMonth = monthId.substring(0, 6);
			// 获取服务器文件
			String filePath = EnvPath.proPath + monthIdMonth + "_" + "V_NATIVE_OM_POST.csv";
			File file = new File(filePath);
			if (file.exists()) {
				try {
					// 设置字符集
					String charset = "UTF-8";
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode("28岗位基本信息原表清单.csv", charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					//将文件转化为文件输入流
					FileInputStream in = new FileInputStream(file);
					//掉zk的浏览器下载方法，进行下载
					Filedownload.save(in, "application/octet-stream", encodedName);
				} catch (FileNotFoundException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (IOException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (Exception e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				}
			} else {
				ZkUtil.showError("下载清单失败,请联系管理员", "温馨提示");
			}
		} else {
			ZkUtil.showError("暂不支持日表清单下载", "温馨提示");
		}
	}
}
